<template>
<div>
  <div id="printHtml" v-html="printDataHtml" style="position: absolute;top: -9999px;left: -9999px;" ></div>
  <el-dialog destroy-on-close close-on-press-escape v-model="showPrintPanel" title="打印小票" @close="hide" width="350px">
    <div v-if="showTitle" style="text-align: center;font-size: 22px;">装车成功</div>
    <div style="margin-top: 60px;text-align: center;">
      <el-button   @click="toPrint(1)" type="primary" style="width: 180px;height: 50px;">打印 小票</el-button>
    </div>
    <div style="height: 100px;"></div>
    <div style="margin-top: 50px;text-align: center;margin-bottom: 50px;" v-if="showPrintAdd">
      <lbtn   @click="toPrint(4)"  style=" width: 180px;height: 50px;" ><i class="fa fa-plus"  /> 新增 装车</lbtn >
    </div>
  </el-dialog>
</div>
</template>

<script>
import tool from "@/utils/tool";
export default {
  name: "doprint",
  props: {
    showTitle: {type: Boolean, default: false},
    showPrintBill: {type: Boolean, default: true},
    showPrintBoth: {type: Boolean, default: true},
    showPrintAdd: {type: Boolean, default: false},
  },
  data () {
    return {
      printDataHtml: '',
      showPrintPanel: false, // 选择打印类型
      printItem: {},
      sendPrintName: '',
      salePrintName: '',
      printView: false,
      printDetail: [] // 需要打印明细的租户
    }
  },
  created () {
    this.getPrintName()
    tool.post('deliver/printDetail', {}).then(ret => {
      for (let i = 0; i < ret.data.length; i++) {
        if (ret.data[i].print) {
          this.printDetail.push(ret.data[i].tenantId)
        }
      }
    })
  },
  methods: {
    // 取打印机名称
    getPrintName () {
      tool.post('deliver/getPrintName', {}).then(ret => {
        if (ret.stat) {
          if (ret.data.length === 0) return tool.err('没有发现打印机')
          let sendPrints = []
          let salePrints = []
          for (let i = 0; i < ret.data.length; i++) {
            if (ret.data[i].sendPrintName) sendPrints.push(ret.data[i].sendPrintName.trim())
            if (ret.data[i].salePrintName) salePrints.push(ret.data[i].salePrintName.trim())
          }
          const LODOP = (typeof getCLodop === 'function') ? getCLodop() : ''
          if (LODOP) {
            let c = LODOP.GET_PRINTER_COUNT()
            if (!c) return tool.err('未发现打印机')
            for (let i = 0; i < c; i++) {
              let name = LODOP.GET_PRINTER_NAME(i)
              if (!name) continue
              if (sendPrints.indexOf(name.trim()) !== -1) {
                this.sendPrintName = name
              }
              if (salePrints.indexOf(name.trim()) !== -1) {
                this.salePrintName = name
              }
            }
          }
        }
      })
    },
    // 打印
    print (item) {
      // 同时打印票据 和 送货单
      const LODOP = (typeof getCLodop === 'function') ? getCLodop() : ''
      if (LODOP) {
        tool.post('deliver/getPrint', {data: item,sendPrintName:this.sendPrintName}).then(ret => {
          if(ret.stat) {
            this.printDataHtml = ret.data.data
            this.printView = ret.data.printView // 是否打印预览
            this.showPrintPanel = true
          }
        })
        this.printItem = item //要打印的数据，本地传入
      }
      else {
        tool.err('没有打印驱动')
      }
    },
    toPrint (index) {
      // let item = this.printItem
      if(index === 1) {
        if (!this.sendPrintName) return tool.err('没有设置小票打印机')
        this.sendPrint()
      }
      if (index === 4) {
        this.$emit('addNew')
        setTimeout(() => {
          this.showPrintPanel = false
        }, 50)
      }
    },
    // 打印小票
    sendPrint() {
      let dom = document.getElementById("printHtml");
      setTimeout(() => {
        let rects = dom.getClientRects()
        let barTop = tool.minus(rects[0].height, 120)
        if (barTop < 300) barTop += 20
        // LODOP.ADD_PRINT_BARCODE(barTop+'px', '90px', '100px', '100px', 'QRCode', '180001-' + item.id);
        this.top = barTop
        LODOP.SET_PRINT_MODE('CATCH_PRINT_STATUS', true)
        LODOP.PRINT_INIT("送货单小票打印");
        LODOP.SET_LICENSES("","C748D766DD08B5AE84E6BC2F3A546E6C7C9","","");
        LODOP.SET_PRINT_PAGESIZE(3, '80mm',45,"") //这里3表示纵向打印且纸高“按内容的高度”；1385表示纸宽138.5mm；45表示页底空白4.5mm
        LODOP.ADD_PRINT_TABLE(40, 0, 800, rects[0].height, this.printDataHtml)
        if (LODOP.SET_PRINTER_INDEX(this.sendPrintName)) {
          if(this.printView) {
            LODOP.PREVIEW();
          }
          else {
            LODOP.PRINT();
          }
          this.showPrintPanel = false
          if (LODOP.CVERSION) {
            CLODOP.On_Return = (TaskID,Value)=> {
              if (Value !== 0 || Value !== false) {
                tool.post('print/billPrintSuc', {cargoId: this.printItem.id}).then(ret => {})
              }
            }
          }

        } else {
          tool.err('读取小票打印机失败')
        }
      }, 50)
    },
    hide () {
      this.$emit('hide')
    }
  },
}
</script>

<style scoped>

</style>
